package com.Jian.reggie.service.impl;

import com.Jian.reggie.common.CustomException;
import com.Jian.reggie.impl.Dish;
import com.Jian.reggie.impl.Setmeal;
import com.Jian.reggie.service.DishService;
import com.Jian.reggie.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.Jian.reggie.impl.Category;
import com.Jian.reggie.service.CategoryService;
import com.Jian.reggie.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 12646
 * @description 针对表【category(菜品及套餐分类)】的数据库操作Service实现
 * @createDate 2023-11-27 14:30:35
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService {
  
  @Autowired
  private DishService dishService;
  @Autowired
  private SetmealService setmealService;
  
  @Override
  public void remove(String id) {
    // 查询当前分类是否关联了菜品，如果关联了菜品，则抛出业务异常
    LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
    dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
    if (dishService.count(dishLambdaQueryWrapper) > 0 ) {
      throw new CustomException("当前分类关联了菜品，无法删除");
      // throw new RuntimeException("当前分类关联了菜品，无法删除");
    }
    // 查询当前分类是否关联了套餐，如果关联了套餐，则抛出业务异常
    LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
    setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
    if (setmealService.count(setmealLambdaQueryWrapper) > 0) {
      // throw new RuntimeException("当前分类关联了套餐，无法删除");
      throw new CustomException("当前分类关联了套餐，无法删除");
    }
    
    // 删除分类
    this.removeById(id);
  }
}




